package com.atguigu.spzx.manager.task;


import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Henry
 * @version 1.0
 * @program: spzx-parent
 * @create: 2025/6/13 下午4:54
 */

@Component
@Slf4j
public class OrderStatisticsTask {

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    //每天凌晨2点，查询前一天日期统计数据，将统计数据加入统计结果表里面
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics(){
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        if(orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics) ;
        }
    }


}
